module PrintProductHelper
  def form_field(product, field, store_product)
    model = product.model.downcase
    column_type = field.column_type
    name = field.field_name
    required = field.is_required
	
    field_id = get_field_id(model, name)
    field_name = "#{model}[#{name}]"
    # 利用反射机制取得对象中与field同名的属性值
    field_value = store_product.nil? ? "" : eval("store_product.get_prop_value(\"#{name}\")")
    case column_type
      when 'text_area'
        content = text_area_tag field_name, field_value, {:id => field_id}
		set_required model, name, content, field.is_required
      when 'text_field'
        content = text_field_tag field_name, field_value, {:id => field_id}
		set_required model, name, content, field.is_required
      when 'select'
        values = []
        field.values.inject(values) {|values, value| values << value.to_s}
        select_tag field_name, options_for_select(values, field_value), {:id => field_id}
      else
        'none'
      end
  end
  
  private
  
  # 为必填字段设置标记
  def set_required(model, name, content, required)
	if required
		content = content + '' + '*'
		# 放置信息的容器
		content = content + "<span id=\"#{name}_message\"></span>"
		# 调用验证逻辑的js
		content = content + "<script language=\"javascript\">set_field_validate(\"#{model}\", \"#{name}\", \"#{get_field_id(model, name)}\");</script>"
	end
	content
  end
  
  # 获取field的id
  def get_field_id(model, name)
	model + "_" + name
  end
end